/**
 *  Eclipse Public License 1.0
 */
package org.fireblade.easysms;

import java.io.BufferedWriter;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Get the index page
 */
public class HttpActionIndex {

  /**
   * @param values
   * @param context
   * @param writer
   * @throws Exception
   */
  public static void process(Context context, Map<String, String> values, BufferedWriter writer)
    throws Exception {

    writer.write("<html>");
    writer.write("  <head>");
    writer.write("    <title>EasySMS " + context.getResources().getString(R.string.version_name) + "</title>");
    writer.write("    <link rel=\"stylesheet\" href=\"/style.css\" type=\"text/css\" media=\"screen\" />");
    writer.write("    <link href=\"/favicon.ico\" rel=\"shortcut icon\" />");
    writer.write("  </head>");
    writer.write("    <script language=\"javascript\" type=\"text/javascript\">");
    writer
      .write("      var link1 = \""
        + context.getResources().getString(R.string.index_conversations)
        + " - <a href=\\\"/index.html\\\" onClick=\\\"document.getElementById('options').innerHTML=link2;window.threads.location.href='/addressbook?';window.showthread.location.href='/newmessage?';return false;\\\">"
        + context.getResources().getString(R.string.index_createnewmessage) + "</a>\";");
    writer
      .write("      var link2 = \""
        + context.getResources().getString(R.string.index_newmessage)
        + " - <a href=\\\"/index.html\\\" onClick=\\\"document.getElementById('options').innerHTML=link1;window.threads.location.href='/getthreads?';window.showthread.location.href='/showthread?';return false;\\\">"
        + context.getResources().getString(R.string.index_toconversations) + "</a>\";");
    writer.write("    </script>");
    int pixel = 80;
    if (MainActivity.DONATE_VERSION) {
      pixel = 45;
    }
    writer.write("  <body onLoad=\"document.getElementById('threads').style.height=document.body.clientHeight-"
      + pixel + "; " + "document.getElementById('showthread').style.height=document.body.clientHeight-" + pixel
      + ";  document.getElementById('options').innerHTML=link1\" bgcolor=\"#DDDDDD\">");
    writer.write("    &nbsp;&nbsp;&nbsp;<b><span id=\"options\" name=\"options\"></span></b>");
    // writer
    // .write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    // writer.write("<a href=\"/index.html\" onClick=\"return deleteByD\">Delete</a>");
    writer
      .write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    writer.write("<a href=\"http://code.google.com/p/androideasysms/\" target=\"blank\">"
      + context.getResources().getString(R.string.index_help_link) + "</a>");
    writer
      .write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
    
    SharedPreferences pref = context.getSharedPreferences(MainActivity.EASY_SMS_SP, Context.MODE_PRIVATE);
    int updateInterval = pref.getInt(MainActivity.UPDATE_INTERVAL, 0);
    writer.write("      <iframe src=\"about:blank\" width=\"1\" height=\"1\" id=\"updatepoller\""
      + " name=\"updatepoller\" frameborder=0 style=\"border:0px\">"+
      (updateInterval != 0 ? "Init Autoupdate" : "") + "</iframe>");
    writer.write("    <br/>");
    writer
      .write("    <iframe src=\"/getthreads?\" width=\"30%\" height=\"500\" id=\"threads\" name=\"threads\" frameborder=0 style=\"border:1px solid black\">This should not be visible - use Mozilla Firefox!</iframe>");
    writer
      .write("    <iframe src=\"\" width=\"68%\" height=\"500\" id=\"showthread\" name=\"showthread\" frameborder=0 style=\"border:1px solid black\">This should not be visible - use Mozilla Firefox!</iframe>");
    if (!MainActivity.DONATE_VERSION) {
      writer.write("<center><b><a href=\"/donate.html\" target=\"blank\">"
        + context.getResources().getString(R.string.index_donate_link) + "</a></b></center>");
    }

    if (0 != updateInterval) {
      writer.write("  <script language=\"javascript\" type=\"text/javascript\">");
      writer.write("    var stopped = false;");
      writer.write("    function checkUpdate() {");
      writer.write("      if (!stopped) {");
      writer.write("        window.updatepoller.location.href='/checkUpdate';");
      writer.write("        setTimeout(\"checkUpdate()\", " + updateInterval * 1000 + ");");
      writer.write("      }");
      writer.write("    }");
      writer.write("    setTimeout(\"checkUpdate()\", " + updateInterval * 1000 + ");");
      writer.write("  </script>");
    }
    
    writer.write("  </body>");
    writer.write("</html>");
  }
}
